package nc.vo.so.m33.pub.biz.vocheck;

import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.calculator.data.RelationItemForCal;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.pubapp.pattern.pub.MathTool;
import nc.vo.so.pub.calculator.INumPriceMnyCheckData;
import nc.vo.so.pub.calculator.SOVODataSetForCal;
import nc.vo.trade.checkrule.VOChecker;

public class NumPriceMnyPubCheck<T extends AbstractBill>
  extends Object
{
  public void checkData(T[] vos) {
    SOVODataSetForCal data = null;
    RelationItemForCal relationItemForCal = new RelationItemForCal();
    for (T vo : vos) {
      for (CircularlyAccessibleValueObject bvo : vo.getChildrenVO()) {
        data = new SOVODataSetForCal(vo.getParentVO(), bvo, relationItemForCal);
        check(data);
      } 
    } 
  }

  
  private void check(INumPriceMnyCheckData checkData) {
    boolean blargessflag = false;

    
    if (ValueUtils.getBoolean(checkData.getBlargessflag())) {
      blargessflag = true;
      
      if (ValueUtils.getBoolean(checkData.getBdiscountflag())) {
        checkDiscount(checkData);
      } else {
        
        checkLargess(checkData);
      } 
    } 
    
    boolean blaborflag = false;
    if (ValueUtils.getBoolean(checkData.getBlaborflag())) {
      blaborflag = true;
      checkLabor(checkData);
    } 
    
    boolean bdiscountflag = false;
    if (ValueUtils.getBoolean(checkData.getBdiscountflag())) {
      bdiscountflag = true;
      checkDiscount(checkData);
    } 
    
    if (!blargessflag && !blaborflag && !bdiscountflag) {
      checkNormal(checkData);
    }
  }




  
  private void checkDiscount(INumPriceMnyCheckData checkData) {
    String msg = NCLangRes4VoTransl.getNCLangRes().getStrByID("4006010_0", "04006010-0121");
    
    if (!ValueUtils.getBoolean(checkData.getBlargessflag())) {
      checkMny(checkData, msg);
    }
  }

  
  private void checkLabor(INumPriceMnyCheckData checkData) {
    String msg = NCLangRes4VoTransl.getNCLangRes().getStrByID("4006010_0", "04006010-0122");
    
    checkNumNull(checkData, msg);
    if (!ValueUtils.getBoolean(checkData.getBlargessflag())) {
      checkMny(checkData, msg);
    }
  }


  
  private void checkLargess(INumPriceMnyCheckData checkData) {
    String msg = NCLangRes4VoTransl.getNCLangRes().getStrByID("4006010_0", "04006010-0123");
    
    checkNum(checkData, msg);
    checkPriceNull(checkData, msg);
    checkMnyNull(checkData, msg);
  }
  
  private void checkMny(INumPriceMnyCheckData checkData, String msg) {
    if (MathTool.isZero(checkData.getNorigtaxmny())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0124", null, new String[] { msg }));

    
    }
    else if (MathTool.isZero(checkData.getNorigmny())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0125", null, new String[] { msg }));
    } 
  }









  
  private void checkMnyNull(INumPriceMnyCheckData checkData, String msg) {
    if (VOChecker.isEmpty(checkData.getNorigtaxmny())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0127", null, new String[] { msg }));

    
    }
    else if (VOChecker.isEmpty(checkData.getNorigmny())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0128", null, new String[] { msg }));
    } 
  }










  
  private void checkNormal(INumPriceMnyCheckData checkData) {
    String msg = NCLangRes4VoTransl.getNCLangRes().getStrByID("4006010_0", "04006010-0129");
    
    checkNum(checkData, msg);
//    checkPrice(checkData, msg);
//    checkMny(checkData, msg);
  }
  
  private void checkNum(INumPriceMnyCheckData checkData, String msg) {
    if (MathTool.isZero(checkData.getNnum())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0130", null, new String[] { msg }));
    }
  }


  
  private void checkNumNull(INumPriceMnyCheckData checkData, String msg) {
    if (VOChecker.isEmpty(checkData.getNnum())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0131", null, new String[] { msg }));
    }
  }



  
  private void checkNumPriceMny(INumPriceMnyCheckData checkData) {
    UFDouble temp = MathTool.add(checkData.getNmny(), checkData.getNtax());
    if (!MathTool.equals(temp, checkData.getNtaxmny()))
    {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0081"));
    }
  }

  
  private void checkPrice(INumPriceMnyCheckData checkData, String msg) {
    if (MathTool.isZero(checkData.getNorigtaxnetprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0132", null, new String[] { msg }));

    
    }
    else if (MathTool.isZero(checkData.getNorignetprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0133", null, new String[] { msg }));

    
    }
    else if (MathTool.isZero(checkData.getNorigtaxprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0134", null, new String[] { msg }));

    
    }
    else if (MathTool.isZero(checkData.getNorigprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0135", null, new String[] { msg }));
    } 
  }


  
  private void checkPriceNull(INumPriceMnyCheckData checkData, String msg) {
    if (VOChecker.isEmpty(checkData.getNorigtaxnetprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0136", null, new String[] { msg }));

    
    }
    else if (VOChecker.isEmpty(checkData.getNorignetprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0137", null, new String[] { msg }));

    
    }
    else if (VOChecker.isEmpty(checkData.getNorigtaxprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0138", null, new String[] { msg }));

    
    }
    else if (VOChecker.isEmpty(checkData.getNorigprice())) {
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4006010_0", "04006010-0139", null, new String[] { msg }));
    } 
  }
}
